<?php

/**
 * XHTML_Option, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Option class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Common_Element.php";
include_once "XHTML__Select_Content.php";

/**
 * The XHTML_Option class is a handler for the option xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Option extends XHTML_Common_Element implements XHTML__Select_Content {
    
    // xhtml attributes
    
    /**
     * Specifies that the option should appear selected (will be displayed 
     * first in the list).
     * 
     * @var boolean
     */
    protected $selected;
    
    /**
     * Specifies that the option should be disabled when it first loads.
     *
     * @var string
     */
    protected $disabled;
    
    /**
     * Defines a label to use when using <optgroup>.
     *
     * @var string
     */
    protected $label;
    
    /**
     * Defines the value of the option to be sent to the server.
     *
     * @var string
     */
    protected $value;
    
    /**
     * The inner text of this tag.
     *
     * @var string
     */
    protected $text;
    
    /**
     * Specifies if the option should appear selected (will be displayed 
     * first in the list).
     *
     * @param boolean $selected
     */
    function setSelected(boolean $selected)
    {
        $this->selected = $selected;
    }
    
    /**
     * Specifies that if option is going to appear selected (will be displayed 
     * first in the list).
     *
     * @return boolean
     */
    function isSelected()
    {
        return $this->selected;
    }
    
    /**
     * Specifies if the option should be disabled when it first loads.
     *
     * @param boolean $disabled
     */
    function setDisabled(boolean $disabled)
    {
        $this->disabled = $disabled;
    }
    
    /**
     * Specifies if the option is going to be disabled when it first loads.
     *
     * @return boolean
     */
    function isDisabled()
    {
        return $this->disabled;
    }
    
    /**
     * Sets the label to use when using <optgroup>.
     *
     * @param string $label
     */
    function setLabel($label)
    {
        $this->label = $label;
    }
    
    /**
     * Gets the label to use when using <optgroup>.
     *
     * @return string
     */
    function getLabel()
    {
        return $this->label;
    }
    
    /**
     * Sets the value of the option to be sent to the server.
     *
     * @param string $value
     */
    function setValue($value)
    {
        $this->value = $value;
    }
    
    /**
     * Gets the value of the option to be sent to the server.
     *
     * @return string
     */
    function getValue()
    {
        return $this->value;
    }
    
    /**
     * Sets the inner text of the tag. e.g.: <option>text</option>
     *
     * @param string $text
     */
    function setText($text)
    {
        $this->text = $text;
    }
    
    /**
     * Gets the inner text of the tag. e.g.: <option>text</option>
     *
     * @return string
     */
    function getText()
    {
        return $this->text;
    }

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<option".parent::toString();
        
        if ($this->selected) {
            $auxString = $auxString."selected=\"selected\" ";
        }
        
        if ($this->disabled) {
            $auxString = $auxString."disabled=\"disabled\" ";
        }
        
        if (isset($this->label)) {
            $auxString = $auxString."label=\"".$this->label."\" ";
        }
        
        if (isset($this->value)) {
            $auxString = $auxString."value=\"".$this->value."\" ";
        }
        
        $auxString = $auxString.">";
        
        /*
         * Addition of the inner text of the option tag to the return 
         * string.
         */
        $auxString = $auxString.$this->text;
        
        $auxString = $auxString."</option>\n";
        
        return $auxString;
    }
    
}

?>